package com.bang.concurrency.example.lock;


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;


@Slf4j
public class LockExample5 {


    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ReentrantLock lock = new ReentrantLock();
        Condition condition = lock.newCondition();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                lock.lock();
                log.error("线程1 获得锁");
                try {
                    log.error("wait a single ");
                    condition.await();
                    log.error("get a single  continue -------->>>>>>");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        });

        executorService.execute(new Runnable() {

            @Override
            public void run() {
                try {
                Thread.sleep(3000);
                lock.lock();
                log.error("线程2 获得锁");

                    Thread.sleep(3000);
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    condition.signalAll();
                    log.error("开始唤醒线程---->>>>");
                    lock.unlock();
                }
            }
        });
        executorService.shutdown();
    }

}
